/* CsJni - .NET / Java Bridge
 * 
 * Copyright 2010 Robert E. Burke Licensed under the Apache License, Version 
 * 2.0 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable 
 * law or agreed to in writing, software distributed under the License is 
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 * KIND, either express or implied. See the License for the specific language 
 * governing permissions and limitations under the License. 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace CsJni.PlatformAbstraction
{
    internal class LinuxPlatform : AbstractUnixPlatform
    {
        public LinuxPlatform(JavaVMConfiguration cfg)
            : base(cfg)
        {
        }

        public override string PlatformName
        {
            get
            {
                return "Linux";
            }
        }

        // http://java.sun.com/javase/6/webnotes/trouble/TSG-VM/html/signals.html
        // http://www.mail-archive.com/mono-devel-list@lists.ximian.com/msg15453.html
        // http://javajiggle.com/2008/01/06/if-jni-based-application-is-crashing-check-signal-handling/
        // export LD_PRELOAD=/usr/lib/jvm/jre-1.6.0/lib/i386/libjsig.so;
        // unset LD_PRELOAD

        /*
      define PATHS_TO_SERVER_JVM "lib/i386/server/libjvm.so"
#      define PATHS_TO_CLIENT_JVM "lib/i386/client/libjvm.so"
         */
        // OpenSuse - /usr/lib/jvm/jre-1.6.0/lib/i386/client/libjvm.so
        // Ubuntu - /usr/lib/jvm/java-6-sun/jre/lib/i386/client/libjvm.so

        // OpenSuse - /usr/lib/jvm/java-1.6.0-sun-1.6.0/jre/lib/i386/client/libjvm.so
        // Ubuntu -   /usr/lib/jvm/java-6-sun-1.6.0.15/jre/lib/i386/client/libjvm.so
        // Fedora -   /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/lib/i386/client/libjvm.so
        // Debian -   /usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm.so

        protected override IntPtr raw_dlopen(string fileName, int mode)
        {
            return NativeMethods.dlopen(fileName, mode);
        }

        protected override IntPtr raw_dlsym(IntPtr hModule, string procName)
        {
            return NativeMethods.dlsym(hModule, procName);
        }

        protected override IntPtr raw_dlerror()
        {
            return NativeMethods.dlerror();
        }

        protected override int raw_dlclose(IntPtr hModule)
        {
            return NativeMethods.dlclose(hModule);
        }

        private sealed class NativeMethods
        {
            private NativeMethods()
            {
            }

            [DllImport("libdl.so.2", BestFitMapping = false, ThrowOnUnmappableChar = true)]
            public static extern IntPtr dlopen([MarshalAs(UnmanagedType.LPStr)] string fileName, int mode);

            [DllImport("libdl.so.2", BestFitMapping = false, ThrowOnUnmappableChar = true)]
            public static extern IntPtr dlsym(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string procName);

            [DllImport("libdl.so.2")]
            public static extern IntPtr dlerror();

            [DllImport("libdl.so.2")]
            public static extern int dlclose(IntPtr hModule);
        }
    }
}
